package com.tjvib.pay.wechat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tjvib.Constants;
import com.tjvib.util.LogUtil;
import com.tjvib.util.ToastUtil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI mIWXAPI;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIWXAPI = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        mIWXAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mIWXAPI.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        switch (baseReq.getType()) {

        }
    }

    //第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp baseResp) {
        LogUtil.e("onResp---> " + baseResp.errCode);
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (baseResp.errCode == 0) {
                ToastUtil.show("支付成功");
                LogUtil.e("onResp---> 支付成功" + baseResp.errCode);
            } else if (baseResp.errCode == -1) {
                ToastUtil.show("支付失败");
                LogUtil.e("onResp---> 支付失败" + baseResp.errCode);
            } else if (baseResp.errCode == -2) {
                ToastUtil.show("支付取消");
                LogUtil.e("onResp---> 支付取消" + baseResp.errCode);
            }
            finish();
        }
    }
}

